
package org.openeai.lite.esb.components;

import java.util.Collection;

import org.mule.api.MuleContext;
import org.mule.api.MuleEventContext;
import org.mule.api.config.MuleConfiguration;
import org.mule.management.stats.FlowConstructStatistics;
import org.openeai.lite.common.util.DateTimeUtils;


public class StatsComponent implements org.mule.api.lifecycle.Callable
{

    public Object onCall(MuleEventContext context) throws Exception
    {
        return getMuleStats(context.getMuleContext());
    }

    public String getMuleStats(MuleContext muleContext)
    {

        Long date = muleContext.getStartDate();
        MuleConfiguration config = muleContext.getConfiguration();
        Collection<FlowConstructStatistics> flows = muleContext.getStatistics()
                        .getServiceStatistics();

        StringBuilder str = new StringBuilder();

        str.append("Mule General Statistics\n");
        str.append("----------------------------------------\n");
        str.append("Status              : Active\n");
        str.append("Mule Identifier     : " + config.getId() + "\n");
        str.append("Domain ID           : " + config.getDomainId() + "\n");
        str.append("Start date/time     : " + new DateTimeUtils().millisToFormattedDate(date)
                        + "\n");
        str.append("Standalone mode?    : " + config.isStandalone() + "\n");
        str.append("Container mode?     : " + config.isContainerMode() + "\n");
        str.append("Client mode?        : " + config.isClientMode() + "\n");
        str.append("\n");
        str.append("\n");
        str.append("Deployed flows (" + flows.size() + ")\n");
        str.append("----------------------------------------\n");
        for (FlowConstructStatistics flowstat : flows)
        {
            str.append("Flow name           : " + flowstat.getName() + "\n");
            str.append("Active?             : " + flowstat.isEnabled() + "\n");
            str.append("Events received     : " + flowstat.getTotalEventsReceived() + "\n");
            str.append("Events processed    : " + flowstat.getProcessedEvents() + "\n");
            str.append("Execution errors    : " + flowstat.getExecutionErrors() + "\n");
            str.append("Fatal errors        : " + flowstat.getFatalErrors() + "\n");
            str.append("\n");
        }
        str.append("----------------------------------------\n");
        str.append("*** END OF STATISTICS ***\n");

        return str.toString();

    }

}
